//
//  MBSpaceMyParentChildShowCollectionCell.m
//  MiBaoTu
//
//  Created by admin on 16/9/22.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceMyParentChildShowCollectionCell.h"

@interface MBSpaceMyParentChildShowCollectionCell () {
    UIView                      *_bgView;
    UIImageView                 *_userImgView;
    UILabel                     *_nickNameLb;
    
    UIView                      *_contentView;
    UIImageView                 *_contentImgView;
    UILabel                     *_contentNameLb;
    UILabel                     *_contentSizeLb;
    
    UIImageView                 *_liveImgView;
    UIButton                    *_moreBt;
    UIView                      *_lineView;
    UIButton                    *_selectBt;
    UIView                      *_contentAllView;
    
    BOOL                        _isComplilingOrSelect;
}

@property (nonatomic,assign)BOOL isCompliling;   //是否编译
@property (nonatomic,assign)BOOL isALlSelecting; //是否全选

@end
@implementation MBSpaceMyParentChildShowCollectionCell

- (void)awakeFromNib {
    // Initialization code
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    self.backgroundColor = [UIColor clearColor];
    self.backgroundView = nil;
    _isComplilingOrSelect = NO;
    
    //背景
    _bgView = [[UIView alloc]initWithFrame:CGRectZero];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_bgView];
    
    //勾选按钮
    _selectBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _selectBt.backgroundColor = [UIColor clearColor];
    _selectBt.hidden = YES;
    [_selectBt setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
    [_selectBt setImage:[UIImage imageNamed:@"more"] forState:UIControlStateHighlighted];
    [_selectBt addTarget:self action:@selector(onGouXuanToSelectAction:) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:_selectBt];
    
    //大模块 包含音乐标题、大小、类型以及下载图标
    _contentAllView = [[UIView alloc]initWithFrame:CGRectZero];
    _contentAllView.backgroundColor = [UIColor whiteColor];
    [_bgView addSubview:_contentAllView];
    
    //头像
    _userImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _userImgView.backgroundColor = [UIColor clearColor];
    [_contentAllView addSubview:_userImgView];
    
    //昵称
    _nickNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _nickNameLb.backgroundColor = [UIColor clearColor];
    _nickNameLb.textAlignment = NSTextAlignmentLeft;
    _nickNameLb.textColor = UIColorFromHex(0x333333);
    _nickNameLb.font = [UIFont systemFontOfSize:13.f];
    [_contentAllView addSubview:_nickNameLb];
    
    //内容
    _contentView = [[UIView alloc]initWithFrame:CGRectZero];
    _contentView.backgroundColor = UIColorFromHex(0xeeeeee);
    [_contentAllView addSubview:_contentView];
    
    //内容头像
    _contentImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _contentImgView.backgroundColor = [UIColor clearColor];
    [_contentView addSubview:_contentImgView];
    
    //内容标题
    _contentNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _contentNameLb.backgroundColor = [UIColor clearColor];
    _contentNameLb.textAlignment = NSTextAlignmentLeft;
    _contentNameLb.textColor = UIColorFromHex(0x333333);
    _contentNameLb.font = [UIFont systemFontOfSize:13.f];
    [_contentView addSubview:_contentNameLb];
    
    //回复
    _contentSizeLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _contentSizeLb.backgroundColor = [UIColor clearColor];
    _contentSizeLb.textAlignment = NSTextAlignmentLeft;
    _contentSizeLb.textColor = UIColorFromHex(0x333333);
    _contentSizeLb.font = [UIFont systemFontOfSize:13.f];
    [_contentView addSubview:_contentSizeLb];
    
    //正在播放图标
    NSMutableArray *imgs = [[NSMutableArray alloc]init];
    for (int i=1; i<=4; i++) {
        NSString *str = [NSString stringWithFormat:@"bofang2-%d",i];
        UIImage *img = [UIImage imageNamed:str];
        [imgs addObject:img];
    }
    _liveImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _liveImgView.backgroundColor = [UIColor clearColor];
    _liveImgView.animationImages = imgs;
    _liveImgView.animationDuration = .6;
    _liveImgView.animationRepeatCount = MAXFLOAT;
    _liveImgView.hidden = YES;
    [_contentView addSubview:_liveImgView];
    
    //线
    _lineView = [[UIView alloc]initWithFrame:CGRectZero];
    _lineView.backgroundColor = [UIColorFromHex(0x999999) colorWithAlphaComponent:.35];
    [_bgView addSubview:_lineView];
    
    //更多按钮
    _moreBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _moreBt.backgroundColor = [UIColor clearColor];
    [_moreBt setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
    [_moreBt setImage:[UIImage imageNamed:@"more"] forState:UIControlStateHighlighted];
    [_moreBt addTarget:self action:@selector(onMoreToSelectAction) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:_moreBt];
}

#pragma mark - 选中更多按钮
- (void)onMoreToSelectAction
{
    if (_moreBt.isHidden == NO) {
        if (_delegate && [_delegate respondsToSelector:@selector(onSelectPCSItemForMoewWithValue:withView:)]) {
            [_delegate onSelectPCSItemForMoewWithValue:_dataDic withView:self];
        }
    }
}

#pragma mark - 勾选
- (void)onGouXuanToSelectAction:(UIButton *)bt
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectPCSItemForGouXuanWithValue:WithSelect:WithView:)]) {
         bt.selected = !bt.selected;
        [_delegate onSelectPCSItemForGouXuanWithValue:_dataDic WithSelect:bt.selected WithView:self];
    }
}

#pragma mark - Actions
- (void)setIsCompliling:(BOOL)isCompliling
{
    _isCompliling = isCompliling;
    [self setNeedsLayout];
}

- (void)onCompileCellForLayout:(BOOL)isCompile
{
    self.isCompliling = isCompile;
    _isComplilingOrSelect = NO;
    [self setNeedsLayout];
}

- (void)onCompileCellForLayout:(BOOL)isCompile WithAllSelct:(BOOL)isAllSelct
{
    self.isCompliling = isCompile;
    self.isALlSelecting = isAllSelct;
    _isComplilingOrSelect = YES;
    [self setNeedsLayout];
}

- (void)setIsALlSelecting:(BOOL)isALlSelecting
{
    _isALlSelecting = isALlSelecting;
    [self setNeedsLayout];
}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
    [self setNeedsLayout];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setDataDic:(NSDictionary *)dataDic
{
    _dataDic = dataDic;
    [self setNeedsLayout];
}

- (void)onPlayerQinZinXiuMusicAnimation:(BOOL)isPlayer
{
    if (_liveImgView) {
        if (isPlayer) {
            [_liveImgView startAnimating];
            _liveImgView.hidden = NO;
        } else {
            [_liveImgView stopAnimating];
            _liveImgView.hidden = YES;
        }
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //value
    if (_dataDic) {
        if ([_dataDic.allKeys containsObject:@"picture"]) {
            //已下载
            NSString *pic = [_dataDic objectForKey:@"userPic"];
            if (![pic isKindOfClass:[NSNull class]]) {
                [_userImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,pic]] placeholderImage:[UIImage imageNamed:@"story_header_bg"]];
            } else {
                _userImgView.image = [UIImage imageNamed:@"story_header_bg"];
            }
            _nickNameLb.text = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"userNick"]];
            NSString *picture = [_dataDic objectForKey:@"picture"];
            if (![picture isKindOfClass:[NSNull class]]) {
                [_contentImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",picture]] placeholderImage:[UIImage imageNamed:@"show_qzx_bg"]];
            } else {
                _contentImgView.image = [UIImage imageNamed:@"show_qzx_bg"];
            }
            _contentNameLb.text =[NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"title"]];
            _contentSizeLb.text = [NSString stringWithFormat:@"%@M",[_dataDic objectForKey:@"size"]];
        } else {
             NSString *FaceImg = [_dataDic objectForKey:@"FaceImg"];
            if (![FaceImg isKindOfClass:[NSNull class]]) {
                [_userImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,FaceImg]] placeholderImage:[UIImage imageNamed:@"story_header_bg"]];
            } else {
                _userImgView.image = [UIImage imageNamed:@"story_header_bg"];
            }
            _nickNameLb.text = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"NickName"]];

            NSString *CoverPath = [_dataDic objectForKey:@"CoverPath"];
            if (![CoverPath isKindOfClass:[NSNull class]]) {
                [_contentImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,CoverPath]] placeholderImage:[UIImage imageNamed:@"show_qzx_bg"]];
            } else {
                _contentImgView.image = [UIImage imageNamed:@"show_qzx_bg"];
            }
            _contentNameLb.text =[NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"ShowTitle"]];
            NSString *cap = [_dataDic objectForKey:@"Capacity"];
            if (![cap isKindOfClass:[NSNull class]]) {
                
                _contentSizeLb.text = [NSString stringWithFormat:@"%.2fM",[cap doubleValue]];
                
            } else {
                _contentSizeLb.text = @"0M";
            }

        }
    }
    UIImage *selectNoImg = [UIImage imageNamed:@"weigouxuan_fb"];
    UIImage *selectImg = [UIImage imageNamed:@"xuanzhong_zl"];
    [_selectBt setImage:selectNoImg forState:UIControlStateNormal];
    [_selectBt setImage:selectImg forState:UIControlStateSelected];
    _selectBt.selected = (_isComplilingOrSelect ? self.isALlSelecting : !self.isCompliling);
    
   
    UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"bofang2-1"]];
    _liveImgView.image = img;
    
    //frame
    _bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.height);
    _selectBt.frame = CGRectMake((ISIPAD?460*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), (_bgView.height-selectNoImg.size.height)/2., selectNoImg.size.width, selectNoImg.size.height);
    _selectBt.hidden = !_isCompliling;
    
    _contentAllView.frame = CGRectMake((_selectBt.isHidden ? _selectBt.left : _selectBt.right+5), 0, _bgView.width-125*PERCEN, _bgView.height);
    
    _userImgView.frame = CGRectMake(25*PERCEN, 20*PERCEN, 50*PERCEN, 50*PERCEN);
    _userImgView.layer.cornerRadius = _userImgView.height/2.;
    _userImgView.layer.masksToBounds = YES;
    
    _nickNameLb.frame = CGRectMake(_userImgView.right+20*PERCEN, (90*PERCEN-20)/2., 200, 20);
    _contentView.frame = CGRectMake(45*PERCEN, _userImgView.bottom+20*PERCEN, (_orientation==UIDeviceOrientationPortrait?500*PERCEN:600*PERCEN), 80*PERCEN);
    
    _contentImgView.frame = CGRectMake(0, 0, 80*PERCEN, _contentView.height);
    
    float musicWidth = [_contentNameLb.text boundingRectWithSize:CGSizeMake(1000, 20) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_contentNameLb.font} context:NULL].size.width+6;
    _contentNameLb.frame = CGRectMake(_contentImgView.right+40*PERCEN, (_contentView.height-20)/2., musicWidth, 20);
    
    float musicSizeWidth = [_contentSizeLb.text boundingRectWithSize:CGSizeMake(1000, 20) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_contentSizeLb.font} context:NULL].size.width+6;
    _contentSizeLb.frame = CGRectMake(_contentNameLb.right, _contentNameLb.top, musicSizeWidth, 20);
    
    _liveImgView.frame = CGRectMake(_contentSizeLb.right+20*PERCEN, (_contentView.height-img.size.height)/2., img.size.width, img.size.height);
    
    _lineView.frame = CGRectMake((ISIPAD?440*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):0), _bgView.height-1, (ISIPAD?_bgView.width-880*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):_bgView.width), 1);
    
    _moreBt.frame = CGRectMake(_bgView.width-100*PERCEN-(ISIPAD?460*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):0), (_bgView.height-20*PERCEN)/2., 100*PERCEN, 20*PERCEN);
    _moreBt.hidden = !_selectBt.isHidden;
}

@end
